package com.xiaoluo.lottery.dao;

import com.xiaoluo.lottery.entity.Order;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Project : lottery
 * Package : com.xiaoluo.lottery.dao
 * Created by Willian.
 * Date  : 2016/4/6 22:43.
 */
@Component
public class OrderJdbcDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    private static final Logger log= LoggerFactory.getLogger(OrderJdbcDao.class);

    /**
     *
     * @param orderNo
     * @return
     */
    public Order select(String orderNo){
        String sql="SELECT * FROM lottery_order where order_no='"+orderNo+"'";
        try{
            return jdbcTemplate.queryForObject(sql,rowMapper);
        }catch (EmptyResultDataAccessException e){
            log.error("Get empty order:orderNo={}",orderNo);
            e.printStackTrace();
            return null;
        }
    }

    /**
     *
     * @param buType
     * @param extendId
     * @return
     */
    public Order selectByType(String buType,Long extendId){
        String sql="SELECT * FROM lottery_order where bu_type=? and extend_id=?";
        try{
            List<Order> list = jdbcTemplate.query(sql, new Object[]{buType, extendId}, rowMapper);
            if(list != null && list.size() >0){
                return list.get(0);
            }
        }catch (EmptyResultDataAccessException e){
            log.error("Get empty order:buType={},extendId={}",buType,extendId);
            e.printStackTrace();
        }
        return null;
    }

    /**
     *
     * @param order
     * @return
     */
    public int insert(Order order){

        String sql="INSERT INTO lottery_order" +
                "            (" +
                "             body," +
                "             detail," +
                "             order_no," +
                "             open_id," +
                "             total_fee," +
                "             cash_fee," +
                "             cash_fee_type," +
                "             coupon_fee," +
                "             coupon_count," +
                "             coupon_id," +
                "             transaction_id," +
                "             prepay_id," +
                "             extend_id," +
                "             bu_type," +
                "             cb_class," +
                "             attach," +
                "             client_ip," +
                "             remark," +
                "             STATUS," +
                "             create_at)" +
                "VALUES (" +
                "        ?," +
                "        ?," +
                "        ?," +
                "        ?," +
                "        ?," +
                "        ?," +
                "        ?," +
                "        ?," +
                "        ?," +
                "        ?," +
                "        ?," +
                "        ?," +
                "        ?," +
                "        ?," +
                "        ?," +
                "        ?," +
                "        ?," +
                "        ?," +
                "        ?," +
                "        ?);";

        Object[] params=new Object[]{
                order.getBody(),
                order.getDetail(),
                order.getOrderNo(),
                order.getOpenid(),
                order.getTotalFee(),
                order.getCashFee(),
                order.getCashFeeType(),
                order.getCouponFee(),
                order.getCouponCount(),
                order.getCouponId(),
                order.getTransactionId(),
                order.getPrepayId(),
                order.getExtendId(),
                order.getBuType(),
                order.getCbClass(),
                order.getAttach(),
                order.getClientIp(),
                order.getRemark(),
                order.getStatus(),
                order.getCreateAt()
        };
        int i=jdbcTemplate.update(sql,params);
        return i;
    }

    /**
     *
     * @param order
     * @return
     */
    public int callBackUpdate(Order order){
        String sql="UPDATE lottery_order " +
                    "set total_fee=?," +
                    "    cash_fee=?," +
                    "    cash_fee_type=?," +
                    "    coupon_fee=?," +
                    "    coupon_count=?," +
                    "    coupon_id=?," +
                    "    transaction_id=?," +
                    "    status=?," +
                    "    update_at=?," +
                    "    paid_date=? where order_no=?";
        Object[] params=new Object[]{
                order.getTotalFee(),
                order.getCashFee(),
                order.getCashFeeType(),
                order.getCouponFee(),
                order.getCouponCount(),
                order.getCouponId(),
                order.getTransactionId(),
                order.getStatus(),
                order.getUpdateAt(),
                order.getPaidDate(),
                order.getOrderNo()
        };
        return jdbcTemplate.update(sql,params);
    }

    /**
     *
     * @param order
     * @return
     */
    public int update(Order order){
        String sql="UPDATE lottery_order set prepay_id=?,status=?,update_at=?,remark=? where order_no=?";
        Object[] params=new Object[]{
                order.getPrepayId(),order.getStatus(),order.getUpdateAt(),order.getRemark(),order.getOrderNo()
        };
        return jdbcTemplate.update(sql,params);
    }

    /**
     *
     */
    private RowMapper<Order> rowMapper=new RowMapper<Order>() {
        @Override
        public Order mapRow(ResultSet resultSet, int i) throws SQLException {
            Order order=new Order();
            order.setId(resultSet.getLong("id"));
            order.setBody(resultSet.getString("body"));
            order.setDetail(resultSet.getString("detail"));
            order.setOrderNo(resultSet.getString("order_no"));
            order.setOpenid(resultSet.getString("open_id"));

            order.setTotalFee(resultSet.getInt("total_fee"));
            order.setCashFee(resultSet.getInt("cash_fee"));
            order.setCashFeeType(resultSet.getString("open_id"));
            order.setCouponFee(resultSet.getInt("coupon_fee"));
            order.setCouponCount(resultSet.getInt("coupon_count"));
            order.setCouponId(resultSet.getString("coupon_id"));

            order.setTransactionId(resultSet.getString("transaction_id"));
            order.setPrepayId(resultSet.getString("prepay_id"));
            order.setExtendId(resultSet.getLong("extend_id"));
            order.setBuType(resultSet.getString("bu_type"));
            order.setCbClass(resultSet.getString("cb_class"));
            order.setAttach(resultSet.getString("attach"));
            order.setClientIp(resultSet.getString("client_ip"));
            order.setRemark(resultSet.getString("remark"));
            order.setStatus(resultSet.getByte("status"));

            order.setCreateAt(resultSet.getTimestamp("create_at"));
            order.setUpdateAt(resultSet.getTimestamp("update_at"));
            order.setPaidDate(resultSet.getTimestamp("paid_date"));

            return order;
        }
    };
}
